<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}角色`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="用户头像" prop="avatar">
        <UploadImg v-model:image-url="drawerProps.row!.avatar" width="135px" height="135px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip> 头像大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model.trim="drawerProps.row!.userName" placeholder="请填写用户名" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.trim="drawerProps.row!.phone" placeholder="请填写手机号" clearable></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="drawerProps.row!.email" placeholder="请填写邮箱" clearable></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model.trim="drawerProps.row!.nickName" placeholder="请填写昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="drawerProps.row!.sex" placeholder="请选择性别" size="large" style="width: 240px">
          <el-option
            v-for="item in getStrDictOptions(DICT_TYPE.COMMON_SEX)"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="drawerProps.row!.deptId" placeholder="请选择部门" size="large" style="width: 240px">
          <el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="角色" prop="roleIds">
        <el-select v-model="drawerProps.row!.roleIds" placeholder="请选择角色" multiple size="large" style="width: 240px">
          <el-option v-for="item in roleDataList" :key="item.id" :label="item.roleName" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import UploadImg from "@/components/Upload/Img.vue";
import { User } from "@/api/system/user";
import { DICT_TYPE, getStrDictOptions } from "@/utils/dict";
import { simpleList, Dept } from "@/api/system/dept";
import { loadRoleList, Role } from "@/api/system/role";
const rules = reactive({
  userName: [{ required: true, message: "请填写用户名" }],
  phone: [{ required: true, message: "请填写手机号" }],
  email: [{ required: true, message: "请填写邮箱" }],
  nickName: [{ required: true, message: "请填写昵称" }],
  sex: [{ required: true, message: "请填写性别" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUser>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);

const deptList = ref<Dept.ResDept[]>([]);
const roleDataList = ref<Role.ResRole[]>([]);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  const res = await simpleList();
  const roleRes = await loadRoleList();
  roleDataList.value = roleRes.data;
  deptList.value = res.data;
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
